package com.yowasa.xiuxian.engine.callback.activity;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.yowasa.xiuxian.annoations.NOTIFY;
import com.yowasa.xiuxian.engine.callback.CmdCallBackHandler;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.mys.ClickMsgComponent;
import com.yowasa.xiuxian.entity.mys.SendMessage;
import com.yowasa.xiuxian.entity.po.WereWolf;
import com.yowasa.xiuxian.entity.po.XiuxianRedPacket;
import com.yowasa.xiuxian.mapper.XiuxianRedPacketMapper;
import com.yowasa.xiuxian.service.helper.MessageHelper;
import com.yowasa.xiuxian.service.helper.UserHelper;
import com.yowasa.xiuxian.service.helper.WareHoustHelper;
import org.checkerframework.checker.units.qual.A;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@NOTIFY(tag = "狼人杀投票")
public class WereWolfChooseHandler extends CmdCallBackHandler {
    public static final Logger logger = LoggerFactory.getLogger(WereWolfChooseHandler.class);
    @Autowired
    private MessageHelper messageHelper;

    @Autowired
    private WareHoustHelper wareHoustHelper;

    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED)
    synchronized public void execute(ClickMsgComponent message) {
        SendMessage sendMessage = new SendMessage();
        sendMessage.setRoom_id(message.getRoom_id());
        sendMessage.setVilla_id(message.getVilla_id());

        JSONObject jsonObject = JSONUtil.parseObj(message.getExtra());
        String uid = message.getUid();
        Integer number = jsonObject.getInt("number");
        Integer round = jsonObject.getInt("round");
        Integer roomId = jsonObject.getInt("roomId");

        WereWolf userWolf = wareHoustHelper.getUserWolf(roomId, uid);
        if(ObjectUtil.isNull(userWolf) || userWolf.getDead() == 1){
            return;
        }else {
            ArrayList<Integer> idList = Lists.newArrayList(105478, 105480, 105479, 105481, 105482, 105483, 105485, 105486, 105487, 105968, 105969, 105970,119136,120346,120347);
            int i = idList.indexOf(userWolf.getMember());
            if (wareHoustHelper.saveOrUpdateTicket((i+1)+"",number,round,roomId)){
                messageHelper.replyMys(sendMessage,"投票成功");
            }
        }
    }


    public String desc() {
        return "狼人杀投票";
    }


}
